<template>
	<main-content
		:tableHeight.sync="tableHeight"
		:otherDependenComponents="otherDependenComponents"
	>
		<template slot="query">
			<el-form
				ref="form"
				:inline="true"
				:model="queryData"
				label-width="100px"
			>
				<el-form-item :label="`${$t('common.belong_merchant')}:`">
					<merchant-select
						v-model="queryData.merchantId"
						v-symbols
						:placeholder="$t('common.select_all')"
					></merchant-select>
				</el-form-item>

				<el-form-item :label="`${$t('common.updateAt')}:`">
					<el-date-picker
						v-model="operateTime"
						size="medium"
						:picker-options="pickerShortcut2"
						format="yyyy-MM-dd HH:mm:ss"
						type="datetimerange"
						range-separator="-"
						:start-placeholder="$t('common.start_time')"
						:end-placeholder="$t('common.end_time')"
						align="right"
						value-format="timestamp"
						style="width: 375px"
						:clearable="false"
					></el-date-picker>
				</el-form-item>

				<el-form-item :label="`${$t('common.action_module')}:`">
					<el-select
						v-model="queryData.operationItem"
						style="width:150px"
						:placeholder="`${$t('common.select_all')}`"
						:value="queryData.operationItem"
						clearable
					>
						<el-option
							v-for="(item, index) in sections"
							:key="index"
							:value="item"
							:label="item"
						></el-option>
					</el-select>
				</el-form-item>
				<!-- @visible-change="needASelectSection($event)" -->
				<el-form-item :label="`${$t('common.action_page')}:`">
					<el-select
						v-model="queryData.operationPage"
						size="medium"
						:placeholder="$t('common.select_all')"
						clearable
						:value="queryData.operationPage"
						collapse-tags
					>
						<el-option
							v-for="(item, index) in pagesDropdown"
							:key="index"
							:label="item"
							:value="item"
						></el-option>
					</el-select>
				</el-form-item>

				<el-form-item>
					<el-select
						v-model="actionSourceType"
						size="medium"
						style="width: 7rem"
					>
						<el-option
							:label="`操作对象`"
							:value="2"
						></el-option>
						<el-option
							:label="`${$t('common.updateBy')}`"
							:value="0"
						></el-option>
						<el-option
							:label="`${$t('common.updateByIP')}`"
							:value="1"
						></el-option>
					</el-select>
					<el-input
						v-model="actionSource"
						size="medium"
						:placeholder="$t('common.please_enter')"
						style="width: 9rem"
						clearable
					></el-input>
				</el-form-item>

				<el-form-item>
					<el-button
						type="primary"
						icon="el-icon-search"
						:disabled="loading"
						:loading="loading"
						size="medium"
						@click="search"
					>
						{{ $t('common.search') }}
					</el-button>
					<el-button
						icon="el-icon-refresh-left"
						:disabled="loading"
						size="medium"
						@click="reset"
					>
						{{ $t('common.reset') }}
					</el-button>
				</el-form-item>
			</el-form>
		</template>
		<template slot="content">
			<el-table
				v-loading="loading"
				element-loading-spinner="el-icon-loading"
				border
				size="mini"
				class="small-size-table"
				:data="dataList"
				style="width: 100%"
				:max-height="tableHeight"
				highlight-current-row
				header-align="center"
				:header-cell-style="getRowClass"
			>
				<el-table-column
					prop="operationItem"
					align="center"
					:label="$t('common.action_module')"
					min-width="120"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.operationItem || '-' }}
					</template>
				</el-table-column>

				<el-table-column
					prop="operationPage"
					align="center"
					:label="$t('common.action_page')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.operationPage || '-' }}
					</template>
				</el-table-column>

				<el-table-column
					prop="operationType"
					align="center"
					:label="$t('common.operation_type')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.operationType || '-' }}
					</template>
				</el-table-column>

				<el-table-column
					prop="operationFunc"
					align="center"
					:label="$t('common.action_function')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.operationFunc || '_' }}
					</template>
				</el-table-column>

				<el-table-column
					prop="operationTarget"
					align="center"
					:label="$t('common.action_ob')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.operationTarget || '-' }}
					</template>
				</el-table-column>

				<el-table-column
					prop="operator"
					align="center"
					:label="$t('common.updateBy')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.operator || '-' }}
					</template>
				</el-table-column>

				<el-table-column
					prop="operatorType"
					align="center"
					:label="$t('common.action_user_type')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.operatorType || '-' }}
					</template>
				</el-table-column>

				<el-table-column
					prop="merchantName"
					align="center"
					:label="$t('common.belong_merchant')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.merchantName || '-' }}
					</template>
				</el-table-column>

				<el-table-column
					prop="operationIp"
					align="center"
					:label="$t('common.updateByIP')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.operationIp || '-' }}
					</template>
				</el-table-column>

				<el-table-column
					prop="createdAt"
					align="center"
					:label="$t('common.updateAt')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.createdAt || '-' }}
					</template>
				</el-table-column>

				<el-table-column
					prop="beforeModify"
					align="center"
					:label="$t('common.action_before')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<span>
							{{ scope.row.beforeModify || '-' }}
						</span>
					</template>
				</el-table-column>

				<el-table-column
					prop="afterModify"
					align="center"
					:label="$t('common.action_after')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<span>
							{{ scope.row.afterModify || '-' }}
						</span>
					</template>
				</el-table-column>

				<el-table-column
					prop="remark"
					align="center"
					:label="$t('common.remark')"
					min-width="120px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						{{ scope.row.remark || '-' }}
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<el-pagination
				v-show="!!total"
				class="pageValue"
				:current-page.sync="pageNum"
				background
				layout="total, sizes,prev, pager, next, jumper"
				:page-size="pageSize"
				:page-sizes="$store.getters.pageSizes"
				:total="total"
				:pager-count="9"
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
			></el-pagination>
		</template>
	</main-content>
</template>

<script>
import list from '@/mixins/list'
import dayjs from 'dayjs'
import { routerNames } from '@/utils/consts'
import MainContent from '@/components/MainContent/index.vue'
const start = () =>
	dayjs()
		.startOf('day')
		.valueOf()

const end = () =>
	dayjs()
		.endOf('day')
		.valueOf()

export default {
	name: routerNames.proxyActionLog,
	components: {
		MainContent
	},
	mixins: [list],
	data() {
		return {
			sections: [],
			pagesDropdown: [],
			operateTime: [start(), end()],
			tableHeight: 480,
			otherDependenComponents: [],
			actionSource: '',
			actionSourceType: 2,
			loading: false,
			dataList: [],
			queryData: {
				createdAtEnd: '',
				operationIp: '',
				merchantId: '',
				operationItem: '',
				operationPage: '',
				operator: '',
				createdAtStart: ''
			}
		}
	},
	watch: {
	},
	created() {
		this.loadDropdownConfig()
	},
	mounted() {
		if (!this.$store.getters.isZk) {
			this.queryData.merchantId = this.$store.getters.merchantInfo.id
			this.loadData()
		}
	},
	methods: {
		loadData() {
			if (this.actionSourceType === 1) {
				this.queryData.operationIp = this.actionSource
				this.queryData.operator = ''
				this.queryData.operationTarget = ''
			} else if (this.actionSourceType === 0) {
				this.queryData.operationIp = ''
				this.queryData.operationTarget = ''
				this.queryData.operator = this.actionSource
			} else {
				this.queryData.operationIp = ''
				this.queryData.operator = ''
				this.queryData.operationTarget = this.actionSource
			}
			this.loading = true
			this.dataList = []
			this.queryData.createdAtStart = dayjs(this.operateTime[0]).format(
				'YYYY-MM-DD HH:mm:ss'
			)
			this.queryData.createdAtEnd = dayjs(this.operateTime[1]).format(
				'YYYY-MM-DD HH:mm:ss'
			)


			this.$api
				.getAgentActionLogList({ ...this.getParams(this.queryData) })
				.then((res) => {
					this.loading = false
					if (res?.code === 200) {
						const { record, totalRecord } = res.data || {}
						this.dataList = Array.isArray(record)
							? Object.freeze(record)
							: []
						this.total = totalRecord || 0
					} else {
						this.$message({
							message:
								res?.msg || this.$t('common.interface_error'),
							type: 'error'
						})
					}
				})
				.catch(() => (this.loading = false))
		},
		loadDropdownConfig() {
			this.loading = true
			this.sections = []
			this.pagesDropdown = []
			this.$api.getActionLogTypeList().then((res) => {
				this.loading = false
				if (res?.code === 200) {
					this.sections = res?.data?.proxyItems.filter((item) => item !== undefined || item !== null)
					this.pagesDropdown = res?.data?.proxyPages.filter((item) => item !== undefined || item !== null)
				} else {
					this.$message({
						message: res?.msg || this.$t('common.interface_error'),
						type: 'error'
					})
				}
			}).catch(() => {
				this.loading = false
			})
		},
		// needASelectSection(status) {
		// 	if (status && typeof this.sectionCode !== 'number') {
		// 		this.pagesDropdown = []
		// 		this.$message({
		// 			message: this.$t('common.please_select_section'),
		// 			type: 'warning'
		// 		})
		// 	}
		// },
		reset() {
			this.operateTime = [start(), end()]
			this.actionSource = ''
			this.actionSourceType = 2
			this.queryData = {
				createdAtEnd: '',
				operationIp: '',
				merchantId: '',
				operationItem: '',
				operationPage: '',
				operator: '',
				createdAtStart: '',
				pageNum: 1
			}
			this.loadData()
		}
	}
}
</script>

<style lang="scss" scoped>
::v-deep div.specify-main-content {
	text-indent: 0;
	border: none;
	font-size: 14px;
	color: #606266;
	width: 100%;
	overflow: hidden;
	padding: 0 10px;
	text-wrap: nowrap;
	font-family: inherit;
	overflow: ellipsis;
	text-overflow: ellipsis;
}
</style>
